//
//  LXAccountSettingViewController.m
//  LvXi_newVersion
//
//  Created by 诸葛孔明 on 16/8/15.
//  Copyright © 2016年 SJT. All rights reserved.
//............................................................................................//
//...........................................个人中心-账户设置页面...........................................//
//............................................................................................//

#import "LXAccountSettingViewController.h"
#import "LXUserImageViewCell.h"

#import "LXNoHighLightViewCell.h"

#import "LCTabBarController.h"

#import "LXAddressManagerViewController.h"

#import "LXAccountSettingCell.h"

#import "Toast.h"

#import "NSUserDefaults+LXUserDefaults.h"

static NSString *const LXUserImageViewCellID = @"LXUserImageView";
static NSString *const LXAccountSettingCellID = @"LXAccountSettingCell";

@interface LXAccountSettingViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,weak) UITableView *tableView;



@end

@implementation LXAccountSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationItem.hidesBackButton = YES;
    [self setUpTableView];
    
    UIButton * logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutButton setBackgroundColor:[UIColor redColor]];
    logoutButton.size = CGSizeMake(ScreenWidth, 40);
    LCTabBarController * tbvc = (LCTabBarController*)self.navigationController.tabBarController;
    logoutButton.y = ScreenHeight - logoutButton.height-64-(tbvc.tabBarHidden?0:64);
    [logoutButton addTarget:self action:@selector(louout) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logoutButton];
}

-(void)louout{
    [LXUser logout];
    [self.view alertWithTitle:@"提示" mesege:@"退出登录成功" buttons:@[@"知道了"] handle:^(NSInteger buttonIndex) {
        [self.navigationController popToRootViewControllerAnimated:NO];
        [self.navigationController.tabBarController setSelectedIndex:0];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUpNavi];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)setUpNavi{
    [super setUpNavi];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"账户设置";
}

#pragma mark - 初始化tableView
- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = MineRatio(50);
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 90, 0);
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    

    
    [self.tableView registerClass:[LXAccountSettingCell class] forCellReuseIdentifier:LXAccountSettingCellID];
    
    [self.tableView registerClass:[LXUserImageViewCell class] forCellReuseIdentifier:LXUserImageViewCellID];
}

#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        LXUserImageViewCell *userImageViewCell = [tableView dequeueReusableCellWithIdentifier:LXUserImageViewCellID];
        userImageViewCell.userImageView.image = [UIImage imageWithMargin:0.5 marginColor:[UIColor whiteColor] image:[UIImage imageNamed:@"user_icon_default"]];
        userImageViewCell.nameLabel.text = @"头像";
        [userImageViewCell setBackgroundImageViewHeight:50];
        userImageViewCell.roundStyle = RoundStyleTop;
        userImageViewCell.enableUnaderLine = YES;
        
        if(LoginState&&[LXUser shareUser].headerImage.length !=0){
            [userImageViewCell.userImageView sd_setImageWithURL:[NSURL URLWithString:[LXUser shareUser].headerImage] placeholderImage:[UIImage imageNamed:@"user_icon_default"]];
        }
        return userImageViewCell;
        
    }else if (indexPath.row == 1){
        
        LXAccountSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:LXAccountSettingCellID];
        cell.textLabel.text = @"用户名";
        cell.textLabel.x = 58;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.detailTextLabel.text = @"000";
        cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.x = 58;
        [cell setBackgroundImageViewHeight:50];
        cell.roundStyle = RoundStyleNone;
        cell.enableUnaderLine = YES;
        if(LoginState){
            cell.detailTextLabel.text = [LXUser shareUser].username;
        }
        return cell;
        
    }else if (indexPath.row == 2){
        
        LXAccountSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:LXAccountSettingCellID];
        cell.textLabel.text = @"昵称";
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.x = 58;
        cell.detailTextLabel.text = @"善觉堂";
        cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.x = 58;
        [cell setBackgroundImageViewHeight:50];
        cell.roundStyle = RoundStyleNone;
        cell.enableUnaderLine = YES;
        if(LoginState){
            cell.detailTextLabel.text = [LXUser shareUser].nickname.length==0?@"还没有，现在设置？":[LXUser shareUser].nickname;
        }
        return cell;
        
    }else if (indexPath.row == 3){
        
        LXAccountSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:LXAccountSettingCellID];
        cell.textLabel.text = @"性别";
        cell.textLabel.x = 58;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.detailTextLabel.text = @"保密";
        cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
        [cell setBackgroundImageViewHeight:50];
        cell.roundStyle = RoundStyleNone;
        cell.enableUnaderLine = YES;
        if(LoginState){
            cell.detailTextLabel.text = [LXUser shareUser].sex;
        }
        return cell;
        
    }else if (indexPath.row == 4) {
        
        LXAccountSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:LXAccountSettingCellID];
        cell.textLabel.text = @"绑定手机";
        cell.textLabel.x = 58;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        [cell setBackgroundImageViewHeight:50];
        cell.roundStyle = RoundStyleNone;
        cell.enableUnaderLine = YES;
        if(LoginState){
            cell.detailTextLabel.text = [LXUser shareUser].mobile;
        }
        return cell;
        
    }
    
    LXAccountSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:LXAccountSettingCellID];
    cell.textLabel.text = @"收货地址管理";
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.x = 58;
    [cell setBackgroundImageViewHeight:50];
    cell.roundStyle = RoundStyleBottom;
    cell.enableUnaderLine = NO;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{//MARK:头像
            
        }break;
        case 1:{//MARK:用户名
            [Toast showWithMessege:@"用户名作为商城唯一标示，无法更改" duration:2];
        }break;
        case 2:{//MARK:昵称
           [self.view alertWithTitle:@"请输入您的昵称" placeHolder:@"请输入您的昵称" buttons:@[@"确认",@"取消"] handle:^(NSInteger clickIndex, UITextField *textField) {
               if(clickIndex == 0){
                   if(LoginState){
                       [[LXDataManager manager] upDateUserInfoWithPrama:@{@"user_id":User_id,@"nickname":textField.text} complete:^(BOOL success, id res) {
                           if(success){
                               
                               [LXUser upDataUserInfoWith:@{@"nickname":textField.text}];
                               [self.tableView reloadData];
                               [Toast showWithMessege:@"修改成功" distanceToBotton:170 duration:2 hiddenComplete:nil];
                           }
                       }];
                   }
               }
           }];
        }break;
        case 3:{//MARK:性别
            NSArray * arr = @[@"男",@"女",@"保密"];
            [self.view alertWithTitle:@"请选择您的性别" mesege:@"" buttons:@[@"男",@"女",@"保密",@"取消"] handle:^(NSInteger buttonIndex) {
                switch (buttonIndex) {
                    case 0:
                    case 1:
                    case 2:{
                        [[LXDataManager manager] upDateUserInfoWithPrama:@{@"user_id":User_id,@"sex":arr[buttonIndex]} complete:^(BOOL success, id res) {
                            if(success){
                                
                                [LXUser upDataUserInfoWith:@{@"sex":arr[buttonIndex]}];
                                [self.tableView reloadData];
                                [Toast showWithMessege:@"修改成功" distanceToBotton:170 duration:2 hiddenComplete:nil];
                            }
                        }];
                    }
                    case 3:break;
                        break;
                        
                    default:
                        break;
                }
            }];
        }break;
        case 4:{//MARK:绑定手机
        }break;
        case 5:{//MARK:收货地址管理
            LXAddressManagerViewController * addrVC = [[LXAddressManagerViewController alloc] init];
            [self.navigationController pushViewController:addrVC animated:YES];
        }break;
        default:
            break;
    }
}

@end
